﻿Public Class ReservarHabitacion
    Dim rs As ADODB.Recordset
    Dim sql As String
    Private Sub RNHabi_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RNHabi.CheckedChanged

        lstnumeroHabi.Items.Clear()
        CmbTipo.Visible = False
        sql = "SELECT NUME_HABI, ENUSO_HABI FROM(HABITACION) WHERE (ENUSO_HABI = 'NO')"
        rs = coneccion.Execute(sql)
        While rs.EOF <> True
            If rs.EOF = True Then
                Exit While
            End If
            lstnumeroHabi.Items.Add(rs.Fields(0).Value)
            rs.MoveNext()
        End While
        'llamar habitaciones libres y mostrar su numero
    End Sub

    Private Sub RTHabi_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RTHabi.CheckedChanged
        lstnumeroHabi.Items.Clear()
        CmbTipo.Visible = True

        sql = "SELECT HABITACION.NUME_HABI, HABITACION.ENUSO_HABI, TIPO_HABITACION.TIPO_HABITACION FROM (TIPO_HABITACION INNER JOIN HABITACION ON TIPO_HABITACION.CODIGO_TIPO = HABITACION.CODIGO_TIPO) WHERE (HABITACION.ENUSO_HABI = 'NO')"
        rs = coneccion.Execute(sql)
        While rs.EOF <> True
            If rs.EOF = True Then
                Exit While
            End If
            lstnumeroHabi.Items.Add(rs.Fields(0).Value)
            rs.MoveNext()
        End While
        ''Llamar las habitaciones filtrando por el tipo si no hay filtrado mostrar todas
    End Sub

    Private Sub RReserva_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RReserva.CheckedChanged
        GrupoCliente.Visible = True
        GrupoTipo.Visible = True
        GrupoReserva.Visible = True
        GrupoDatos.Visible = False
        limpiarData()
    End Sub

    Private Sub RCancel_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RCancel.CheckedChanged
        GrupoCliente.Visible = False
        GrupoTipo.Visible = False
        GrupoReserva.Visible = False
        GrupoDatos.Visible = True
        limpiarData()

    End Sub

    Private Sub Rnuevo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rnuevo.CheckedChanged
        Dim nuevo As New Registro
        nuevo.Show()
        GrupoDatos.Visible = True
        limpiarData()
    End Sub

    Private Sub Rregistrado_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rregistrado.CheckedChanged
        GrupoDatos.Visible = True
        limpiarData()
    End Sub
    Public Sub limpiarData()
        txtCodigo.Text = ""
        txtAMaterno.Text = ""
        txtAPaterno.Text = ""
        txtNombre.Text = ""
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Dispose()
    End Sub

    Private Sub btnAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
        Dim codigo_reserva As Integer = 0
        sql = "SELECT MAX(CODI_RESE) FROM RESERVACION"
        rs = coneccion.Execute(sql)
        If codigo_reserva <> 0 Then
            codigo_reserva = rs.Fields(0).Value + 1
        Else
            codigo_reserva = 10000
        End If

        Dim Codigo_Habitacion As String = ""

        If RCancel.Checked Then
            sql = "DELETE FROM RESERVACION WHERE (RESERVACION.CODI_CLIEN = '" & txtCodigo.Text & "')"
            rs = coneccion.Execute(sql)
            ''borrar registro
        Else
            sql = "SELECT CODI_HABI, NUME_HABI FROM(HABITACION) WHERE (NUME_HABI = '" & lstnumeroHabi.SelectedItem.ToString & "')"
            rs = coneccion.Execute(sql)
            If rs.EOF <> True Then
                rs.MoveFirst()
                Codigo_Habitacion = rs.Fields(0).Value.ToString
            End If

            sql = "INSERT INTO RESERVACION (CODI_CLIEN, CODI_HABI, FEIN_RESE, CODI_RESE, MEDI_RESE) VALUES ('" & txtCodigo.Text & "','" & Codigo_Habitacion & "','" & FechaActual.Value.ToString & "','" & codigo_reserva.ToString & "','" & CmbMedio.SelectedItem.ToString & "')"
            rs = coneccion.Execute(sql)
            ''asignar registro
        End If
        ''Enviar data a la BD
        Me.Dispose()
    End Sub

    Private Sub CmbTipo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbTipo.SelectedIndexChanged
        lstnumeroHabi.Items.Clear()
        sql = "SELECT HABITACION.NUME_HABI, HABITACION.ENUSO_HABI, TIPO_HABITACION.TIPO_HABITACION FROM (TIPO_HABITACION INNER JOIN HABITACION ON TIPO_HABITACION.CODIGO_TIPO = HABITACION.CODIGO_TIPO) WHERE (TIPO_HABITACION.TIPO_HABITACION = '" & CmbTipo.SelectedItem.ToString & "') AND (HABITACION.ENUSO_HABI = 'NO')"
        rs = coneccion.Execute(sql)
        While rs.EOF <> True
            If rs.EOF = True Then
                Exit While
            End If
            lstnumeroHabi.Items.Add(rs.Fields(0).Value)
            rs.MoveNext()
        End While
        ''REfinar parametros de llamada a base de datos
    End Sub

    Private Sub ReservarHabitacion_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        conectar()
        sql = "SELECT TIPO_HABITACION FROM TIPO_HABITACION"
        rs = coneccion.Execute(sql)
        While rs.EOF <> True
            If rs.EOF = True Then
                Exit While
            End If
            CmbTipo.Items.Add(rs.Fields(0).Value)
            rs.MoveNext()
        End While
    End Sub

    Public Sub llenar(ByVal Resultado As String)
        sql = "SELECT CODI_CLIEN, NOMB_CLIEN, PATE_CLIEN, MATE_CLIEN  FROM CLIENTE WHERE (CODI_CLIEN = '" & Resultado & "')"
        rs = coneccion.Execute(sql)
        txtNombre.Text = rs.Fields(1).Value
        txtAPaterno.Text = rs.Fields(2).Value
        txtAMaterno.Text = rs.Fields(3).Value
    End Sub

    Private Sub btnVerificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVerificar.Click
        Dim mostrar As New Resultados(txtAPaterno.Text, txtAMaterno.Text, txtNombre.Text, txtCodigo.Text)
        mostrar.Show()
    End Sub


    Private Sub ReservarHabitacion_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDoubleClick
        llenar(Resultado)
    End Sub

    Private Sub lstnumeroHabi_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstnumeroHabi.SelectedIndexChanged

    End Sub
End Class